package common

import "fmt"

type ListNode struct {
	Val int
	Next *ListNode
}

func BuildListNode(nums []int) *ListNode {
	size := len(nums)
	var p *ListNode
	for i:=size-1; i>=0; i-- {
		if p == nil {
			p = &ListNode{Val: nums[i]}
			continue
		}
		t := &ListNode{Val: nums[i]}
		t.Next = p
		p = t
	}
	return p
}

func (p *ListNode) Print() {
	t := p
	fmt.Print("[")
	for t != nil {
		fmt.Print(t.Val, ",")
		t = t.Next
	}
	fmt.Println("]")
}

func (p *ListNode) Slice() []int {
	var res []int
	t := p
	for t != nil {
		res = append(res, t.Val)
		t = t.Next
	}
	return res
}